package com.jin.lazytool.rewritte;

import com.jin.lazytool.annotion.CallFun;
import com.jin.lazytool.annotion.CallParFun;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.WeakChangeListener;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.WeakListChangeListener;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Separator;
import javafx.scene.control.SingleSelectionModel;

public class ChoiceBoxSelectModel<T> extends SingleSelectionModel<T>{
    private final ChoiceBox<T> choiceBox;
    private ChangeListener<ObservableList<T>> itemsObserver;
    private ListChangeListener<T> itemsContentObserver;
    private WeakListChangeListener<T> weakItemsContentObserver;

    private CallFun nullCallFun;
    private CallParFun<T> callFun;

    /**
     * @param var1              choiceBox
     * @param nullCallFun       选择为null时回调
     * @param callFun           选中不为null时回调
     */
    public ChoiceBoxSelectModel(ChoiceBox<T> var1,CallFun nullCallFun,CallParFun<T> callFun) {
        this.nullCallFun = nullCallFun;
        this.callFun = callFun;
        if (var1 == null) {
            throw new NullPointerException("ChoiceBox can not be null");
        } else {
            this.choiceBox = var1;
            this.itemsContentObserver = (var1x) -> {
                if (this.choiceBox.getItems() != null && !this.choiceBox.getItems().isEmpty()) {
                    if (this.getSelectedIndex() == -1 && this.getSelectedItem() != null) {
                        int var2 = this.choiceBox.getItems().indexOf(this.getSelectedItem());
                        if (var2 != -1) {
                            this.setSelectedIndex(var2);
                        }
                    }
                } else {
                    this.setSelectedIndex(-1);
                }

            };
            this.weakItemsContentObserver = new WeakListChangeListener(this.itemsContentObserver);
            if (this.choiceBox.getItems() != null) {
                this.choiceBox.getItems().addListener(this.weakItemsContentObserver);
            }

            this.itemsObserver = (var1x, var2, var3) -> {
                if (var2 != null) {
                    var2.removeListener(this.weakItemsContentObserver);
                }

                if (var3 != null) {
                    var3.addListener(this.weakItemsContentObserver);
                }

                this.setSelectedIndex(-1);
                if (this.getSelectedItem() != null) {
                    int var4 = this.choiceBox.getItems().indexOf(this.getSelectedItem());
                    if (var4 != -1) {
                        this.setSelectedIndex(var4);
                    }
                }

            };
            this.choiceBox.itemsProperty().addListener(new WeakChangeListener(this.itemsObserver));
        }
    }

    @Override
    protected T getModelItem(int var1) {
        ObservableList<T> var2 = this.choiceBox.getItems();
        if (var2 == null) {
            return null;
        } else {
            return var1 >= 0 && var1 < var2.size() ? var2.get(var1) : null;
        }
    }
    @Override
    protected int getItemCount() {
        ObservableList var1 = this.choiceBox.getItems();
        return var1 == null ? 0 : var1.size();
    }
    @Override
    public void select(int var1) {
        super.select(var1);
        if (this.choiceBox.isShowing()) {
            this.choiceBox.hide();
        }

    }

    public void select(T var1) {
        super.select(var1);
        if (var1 != null && !this.choiceBox.getItems().contains(var1)) {
            this.setSelectedIndex(-1);
        }
        if(super.getSelectedItem() != null) {
            callFun.call(super.getSelectedItem());
        }else{
            nullCallFun.call();
        }

    }

    public void selectPrevious() {
        for(int var1 = this.getSelectedIndex() - 1; var1 >= 0; --var1) {
            Object var2 = this.getModelItem(var1);
            if (!(var2 instanceof Separator)) {
                this.select(var1);
                break;
            }
        }

    }

    public void selectNext() {
        for(int var1 = this.getSelectedIndex() + 1; var1 < this.getItemCount(); ++var1) {
            Object var2 = this.getModelItem(var1);
            if (!(var2 instanceof Separator)) {
                this.select(var1);
                break;
            }
        }

    }
}
